



#include <iostream>
#include <opencv2/opencv.hpp>
#include "face.h"//来自百度的SDK
#include "time.h"

using namespace std; 
using namespace cv;
using namespace aip;



int main()
{

    VideoCapture cap(0);
    if(!cap.isOpened())
    {
    cout << "open video error\n" << endl;

        return -1;
    }
    else
    {
   // cout << "success\n" << endl;

    }



    Mat frame;
    Mat gray_frame;
    Mat equalizeframe;
    Mat faceframe;

    CascadeClassifier Classifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");

    vector<Rect> allFace;//所有脸的容器

    vector<uchar> jpgbuff;

    std::string app_id = "119047181";
    std::string api_key = "Srf1sRGatO8hqmRM5CGNQASn";        
    std::string secret_key = "QXTXthjwzGNZZXFlPiZh22cZUzW0ttWF";

    aip::Face client(app_id, api_key, secret_key);


    std::string base64Img;
    Json::Value result;

    time_t sec;
    tm* local_time;

    char time_buff[64] = {0};
    while(1)
    {






        cap >> frame;



        cvtColor(frame, gray_frame, CV_BGR2GRAY);//转换成灰度图片
        equalizeHist(gray_frame, equalizeframe);//直方图均衡化

        Classifier.detectMultiScale(equalizeframe, allFace);


        if (allFace.size())
        {
            rectangle(equalizeframe, allFace[0], Scalar(255, 255, 0));
            faceframe = frame(allFace[0]);//提取人脸部分的图片
            imencode(".jpg", faceframe, jpgbuff);//将图片转换成.jpg格式

           base64Img =  base64_encode( (char *)jpgbuff.data(), jpgbuff.size());//将图片转换成base64


            result = client.search(base64Img, "BASE64", "school", aip::null);//发送给百度云的人脸库中

            // // 检查返回结果
            // if (!result.isMember("error_code")) 
            // {
            //     std::cout << "连接成功！服务返回数据: " << result.toStyledString() << std::endl;
            // } 
            // else 
            // {
            //     std::cerr << "连接失败！错误码: " << result["error_code"].asInt() 
            //                 << ", 错误信息: " << result["error_msg"].asString() << std::endl;
            // }



            if(!result["result"].isNull())
            {
                if(result["result"]["user_list"][0]["score"].asInt() > 80)
                {

                    sec = time(NULL);
                    local_time = localtime(&sec);//时间戳转换成时间日期结构体
                    strftime(time_buff, sizeof(time_buff), "%Y-%m-%d-%H:%M-%S", local_time);//将时间转换成结构体

                    cout << result["result"]["user_list"][0]["user_id"]<< "," << std::string(time_buff) << endl;

                   // cout << ctime(&sec) << endl;  
                    
                    putText(equalizeframe, result["result"]["user_list"][0]["user_id"].asString(), 
                            Point(0,50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255,255,0));   //FONT_HERSHEY_SIMPLEX是字体，1.0是字号           
        
                    putText(equalizeframe, std::string(time_buff),//ctime(&sec), 
                            Point(0,100), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255,255,0)); 
                }
            }

            // else
            // {
            //     cout << "result is error\n" << endl;
            // }

        }
        // else
        // {
        //         cout << "there is no face\n" << endl;
        // }   

        imshow("111",equalizeframe);

    // 按ESC键退出
        waitKey(100);

    }


    return 0;

}



//g++  main.cpp -o main -lopencv_highgui -lopencv_core -lopencv_imgproc -lopencv_objdetect -std=c++11 -lcurl -lcrypto -ljsoncpp




